package com.ruoyi.work.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.work.domain.PdWorkList;
import com.ruoyi.work.service.IPdWorkListService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;


/**
 * 派单Controller
 *
 * @author wkw
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/workList")
public class PdWorkListController extends BaseController
{
    @Autowired
    private IPdWorkListService pdWorkListService;

    @PostMapping("/saveAttachment")
    public AjaxResult saveAttachment(@RequestParam("saveAttachment") MultipartFile file) throws Exception
    {
        if (!file.isEmpty()) {
            String url = FileUploadUtils.upload("/pdAttachment/uploadPath", file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 查询派单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PdWorkList pdWorkList)
    {
        startPage();
        List<PdWorkList> list = pdWorkListService.selectPdWorkListList(pdWorkList);
        return getDataTable(list);
    }

    /**
     * 导出派单列表
     */
    @Log(title = "派单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdWorkList pdWorkList)
    {
        List<PdWorkList> list = pdWorkListService.selectPdWorkListList(pdWorkList);
        ExcelUtil<PdWorkList> util = new ExcelUtil<PdWorkList>(PdWorkList.class);
        util.exportExcel(response, list, "派单数据");
    }

    /**
     * 获取派单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pdWorkListService.selectPdWorkListById(id));
    }

    /**
     * 新增派单
     */
    @Log(title = "派单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PdWorkList pdWorkList)
    {
        return toAjax(pdWorkListService.insertPdWorkList(pdWorkList));
    }

    /**
     * 修改派单
     */
    @Log(title = "派单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PdWorkList pdWorkList)
    {
        return toAjax(pdWorkListService.updatePdWorkList(pdWorkList));
    }

    /**
     * 删除派单
     */
    @Log(title = "派单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pdWorkListService.deletePdWorkListByIds(ids));
    }
}
